package com.cyanite.playlv2.util;

import com.cyanite.playlv2.error.LocationException;
import com.cyanite.playlv2api.error.PlayLv2ApiCredentialsException;
import com.cyanite.playlv2api.error.PlayLv2ApiException;
import com.cyanite.playlv2.R;

import android.content.Context;
import android.widget.Toast;

import java.io.IOException;
import java.net.SocketException;
import java.net.SocketTimeoutException;

public class NotificationsUtil {

    public static void ToastReasonForFailure(Context context, Exception e) {

        if (e == null) {
            Toast.makeText(context, R.string.error_unknow,
                    Toast.LENGTH_LONG).show();
        } else if (e instanceof SocketTimeoutException) {
            Toast.makeText(context, R.string.error_overflow, Toast.LENGTH_LONG).show();

        } else if (e instanceof SocketException) {
            Toast.makeText(context, R.string.error_server_no_response, Toast.LENGTH_LONG).show();

        } else if (e instanceof IOException) {
            Toast.makeText(context, R.string.error_network_unavailable, Toast.LENGTH_LONG).show();

        } else if (e instanceof LocationException) {
            Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();

        } else if (e instanceof PlayLv2ApiCredentialsException) {
            Toast.makeText(context, R.string.authorization_failed, Toast.LENGTH_LONG).show();

        } else if (e instanceof PlayLv2ApiException) {
            // Playlv2Error is one of these
            String message;
            int toastLength = Toast.LENGTH_LONG;
            if (e.getMessage() == null) {
                message = context.getString(R.string.error_invalid_request);
            } else {
                message = e.getMessage();
                toastLength = Toast.LENGTH_LONG;
            }
            Toast.makeText(context, message, toastLength).show();

        } else {
            Toast.makeText(context, R.string.error_unknow,
                    Toast.LENGTH_LONG).show();
        }
    }
}
